﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace GerbilLib
{
    /// <summary>
    /// Provides a set of options that can be used to manipulate how MediaGerbil
    /// executes certain functions. This object is also used to serialize and
    /// deserialize the Preferences into/from XML.
    /// </summary>
    public class Preferences
    {
        /// <summary>
        /// Will be set to <c>true</c> if an error occurred during deserialization.
        /// </summary>
        [XmlIgnore]
        public bool DeserializedWithErrors = false;
        /// <summary>
        /// Will be set to <c>true</c> if the schema was upgraded from an older version.
        /// </summary>
        [XmlIgnore]
        public bool UpgradedSchemaVersion = false;

        /// <summary>
        /// Gets or sets the media directories.
        /// </summary>
        /// <value>
        /// The media directories.
        /// </value>
        public List<MediaDirectory> MediaDirectories { get; set; }
        /// <summary>
        /// Gets or sets the excluded media directories.
        /// </summary>
        /// <value>
        /// The excluded media directories.
        /// </value>
        public List<MediaDirectory> ExcludedMediaDirectories { get; set; }
        /// <summary>
        /// Gets or sets the ignored directories.
        /// </summary>
        /// <value>
        /// The ignored directories.
        /// </value>
        public HashSet<string> IgnoredDirectories { get; set; }
        /// <summary>
        /// Gets or sets the diff match patch match threshhold.
        /// </summary>
        /// <value>
        /// The diff match patch match threshhold.
        /// </value>
        public float DiffMatchPatchMatchThreshhold { get; set; }
        /// <summary>
        /// Gets or sets the media file extensions.
        /// </summary>
        /// <value>
        /// The media file extensions.
        /// </value>
        public HashSet<string> MediaFileExtensions { get; set; }
        /// <summary>
        /// Gets or sets the title cleaning options.
        /// </summary>
        /// <value>
        /// The title cleaning options.
        /// </value>
        public TitleCleaningOptions TitleCleaningOptions { get; set; }
        /// <summary>
        /// Gets or sets the title matching options.
        /// </summary>
        /// <value>
        /// The title matching options.
        /// </value>
        public TitleMatchingOptions TitleMatchingOptions { get; set; }
        /// <summary>
        /// Gets or sets the cover image options.
        /// </summary>
        /// <value>
        /// The cover image options.
        /// </value>
        public CoverImageOptions CoverImageOptions { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="Preferences"/> class.
        /// </summary>
        public Preferences()
        {
            // Set up default preferences
            MediaDirectories = new List<MediaDirectory>();
            ExcludedMediaDirectories = new List<MediaDirectory>();
            IgnoredDirectories = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "Sample", "metadata" };
            TitleCleaningOptions = GetDefaultTitleCleaningOptions();
            MediaFileExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { ".avi", ".mkv", ".wmv", ".mp4", ".mov", ".iso" };
            DiffMatchPatchMatchThreshhold = 0.13f;
            TitleMatchingOptions = new TitleMatchingOptions(TitleMatchingOptions.TitleMatchSources.ImdbRatings, TitleMatchingOptions.TitleSortOptions.SortByLevenshtein, sortByNewerYear: true, useYearInPath: true, useYearInParentOnly: false);
            CoverImageOptions = new CoverImageOptions(CoverImageOptions.CoverImageSources.IMDb, false, TmdbImage.ImageSize.Thumb, TmdbImage.ImageSize.Original, TmdbImage.ImageSize.Thumb, TmdbImage.ImageSize.Original);
        }

        /// <summary>
        /// Gets the default title cleaning options.
        /// </summary>
        /// <returns>Default title cleaning options.</returns>
        public static TitleCleaningOptions GetDefaultTitleCleaningOptions()
        {
            return new TitleCleaningOptions(new HashSet<string> { "720p", "1080p", "x264", "bdrip", "brrip", "dvdrip", ".ts", "xvid", "dvdscr", ".limited", ".proper", ".real", ".unrated", ".stv", ".repack", ".extended", ".rerip", "bluray", ".cut", ".director", ".r5", ".docu", ".hdtv", ".retail", ".ws", ".fs", ".dc", ".hdrip", ".ppvrip" }, stripYearFromTitle: true, replacePeriodsWithSpaces: true, removeBrackets: true);
        }
    }
}
